﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using DepSoft.Mobile.WP7.FollwIt.Model;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;

namespace DepSoft.Mobile.WP7.FollwIt
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            if (string.IsNullOrEmpty(FollwItApi.GetInstance().UserName) || string.IsNullOrEmpty(FollwItApi.GetInstance().Password))
                this.MainPagePivot.SelectedIndex = 0; //Set the menu page as default
            else
                this.MainPagePivot.SelectedIndex = 1; //Set the stream page as default
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.MenuListBox.SelectedIndex = -1;
        }

        private void appbarButton_Click(object sender, System.EventArgs e)
        {
            //GC.Collect();
            //GC.SuppressFinalize(this);

            //TODO: When pressing NEXT on the last 'page' of PREVIOUS on the first 'page' we also detach the triggers and this is ok!
            switch (this.MainPagePivot.SelectedIndex)
            {
                case 2:
                    var movieItems = from movie in this.MovieListBox.Items
                                     select this.MovieListBox.ItemContainerGenerator.ContainerFromItem(movie);

                    foreach (var movieItem in movieItems)
                    {
                        ClearImageTriggers(movieItem);
                        //((BitmapImage)coverImage.Source).UriSource = null;
                    }
                    break;
                case 3:
                    var tvShowItems = from tvShow in this.TVShowListBox.Items
                                      select this.TVShowListBox.ItemContainerGenerator.ContainerFromItem(tvShow);

                    foreach (var tvShowItem in tvShowItems)
                    {
                        ClearImageTriggers(tvShowItem);
                        //((BitmapImage)coverImage.Source).UriSource = null;
                    }
                    break;
                default:
                    break;
            }
        }

        private void ClearImageTriggers(DependencyObject movieItem)
        {
            Image coverImage = FindFirstElementInVisualTree<Image>(movieItem);
            var triggers = Interaction.GetTriggers(coverImage);
            foreach (var trigger in triggers)
            {
                foreach (EventToCommand action in trigger.Actions)
                {
                    action.ClearValue(EventToCommand.CommandProperty);
                    action.ClearValue(EventToCommand.CommandParameterProperty);
                    action.Detach();
                }
                trigger.Detach();
            }
            triggers.Clear();
        }

        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(parentElement);
            if (count == 0)
                return null;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parentElement, i);

                if (child != null && child is T)
                {
                    return (T)child;
                }
                else
                {
                    var result = FindFirstElementInVisualTree<T>(child);
                    if (result != null)
                        return result;

                }
            }
            return null;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.MainPagePivot.SelectedIndex = 2;
        }
    }
}